// 2025/8/20
// 字符编码

#include <iostream>
#include <unordered_map>
#include <queue>
using namespace std;

int main() {
    string str;
    while(cin >> str)
    {
        unordered_map<char, long long> times;
        for(auto ch : str)
            times[ch]++;
        priority_queue<long long, vector<long long>, greater<>> pq;
        for(auto& pr : times)
            pq.emplace(pr.second);
        long long ans = 0, tmp;
        while(pq.size() >= 2)
        {
            tmp = pq.top(); pq.pop();
            tmp += pq.top(); pq.pop();
            ans += tmp;
            pq.emplace(tmp);
        }
        cout << ans << endl;
    }
}
// 64 位输出请用 printf("%lld")